package org.jeecg.modules.demo.ysd.controller;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;

public class test {

    public static void main(String[] args) {
        extracted(s1);
    }


    private static void extracted(String s) {
        try {
            // 1. 解析XML
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(s)));

            // 2. 创建XPath对象
            XPath xpath = XPathFactory.newInstance().newXPath();

            // 示例1：获取所有book的title文本
            String expression = "//opticalImageInfo/sourceDataInfo/sourceBase64Data/text()";
            NodeList titles = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
            for (int i = 0; i < titles.getLength(); i++) {
                System.out.println("Title: " + titles.item(i).getNodeValue());
            }

            // 示例2：获取id=2的book的author
//                String author = xpath.evaluate("//book[@id='2']/author/text()", doc);
//                System.out.println("Author of book 2: " + author);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static String s1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
            "<config version=\"1.7\" xmlns=\"http://www.ipc.com/ver10\">\n" +
            "<types>\n" +
            "<smartType>\n" +
            "<enum>MOTION</enum>\n" +
            "<enum>SENSOR</enum>\n" +
            "<enum>PERIMETER</enum>\n" +
            "<enum>TRIPWIRE</enum>\n" +
            "<enum>PEA</enum>\n" +
            "<enum>AVD</enum>\n" +
            "<enum>OSC</enum>\n" +
            "<enum>CPC</enum>\n" +
            "<enum>CDD</enum>\n" +
            "<enum>IPD</enum>\n" +
            "<enum>VFD</enum>\n" +
            "<enum>VEHICE</enum>\n" +
            "<enum>AOIENTRY</enum>\n" +
            "<enum>AOILEAVE</enum>\n" +
            "<enum>PASSLINECOUNT</enum>\n" +
            "<enum>TRAFFIC</enum>\n" +
            "<enum>FIRE</enum>\n" +
            "<enum>TEMPRATURE</enum>\n" +
            "</smartType>\n" +
            "<subscribeOption>\n" +
            "<enum>ALARM</enum>\n" +
            "<enum>FEATURE_RESULT</enum>\n" +
            "<enum>FEATURE_RULE</enum>\n" +
            "</subscribeOption>\n" +
            "</types>\n" +
            "<!--火点监测类型-->\n" +
            "<smartType type=\"openAlramObj\">FIRE</smartType>\n" +
            "<subscribeRelation type=\"subscribeOption\">FEATURE_RULE</subscribeRelation>\n" +
            "<currentTime type=\"tint64\">1668482701705110</currentTime>\n" +
            "<macAddress type=\"string\"><![CDATA[00:18:ae:00:33:37]]></macAddress>\n" +
            "<iveFire>\n" +
            "<fireInfo type=\"list\" count=\"1\">\n" +
            "<item>\n" +
            "<thermalInfo>\n" +
            "<targetId type=\"uint32\">0</targetId>\n" +
            "<status type=\"perStatus\">SMART_NONE</status>\n" +
            "<rect>\n" +
            "<!-- 火点框左上角坐标-->\n" +
            "<x1 type=\"uint32\">4335</x1> \n" +
            "<y1 type=\"uint32\">4114</y1>\n" +
            "<!-- 火点框右下角坐标-->\n" +
            "<x2 type=\"uint32\">5233</x2>\n" +
            "<y2 type=\"uint32\">5936</y2>\n" +
            "<!--获取火点框中心温度方法：\n" +
            "(1)计算中心坐标 [(x1+x2)/2, (y1+y2)/2] \n" +
            "(2)调用API GetDotTemperature获取中心温度 -->\n" +
            "</rect>\n" +
            "</thermalInfo>\n" +
            "</item>\n" +
            "</fireInfo>\n" +
            "</iveFire>\n" +
            "<opticalImageInfo>\n" +
            "<sourceDataInfo>\n" +
            "<dataType type=\"uint32\">0</dataType>\n" +
            "<!--可见光全景图 宽-->\n" +
            "<width type=\"uint32\">1280</width>\n" +
            "<!--可见光全景图 高-->\n" +
            "<height type=\"uint32\">720</height>\n" +
            "<!--可见光全景图 数据长度-->\n" +
            "<sourceBase64Length type=\"uint32\">202388</sourceBase64Length>\n" +
            "<!---可见光全景图 图片数据-->\n" +
            "<sourceBase64Data type=\"string\"><![CDATA[optical full image data ...]]></sourceBase64Data>\n" +
            "</sourceDataInfo>\n" +
            "<listInfo type=\"list\" count=\"0\">\n" +
            "</listInfo >\n" +
            "</opticalImageInfo>\n" +
            "<thermalImageInfo>\n" +
            "<sourceDataInfo>\n" +
            "<dataType type=\"uint32\">0</dataType>\n" +
            "<!--热成像全景图 宽-->\n" +
            "<width type=\"uint32\">1280</width>\n" +
            "<!--热成像全景图 高-->\n" +
            "<height type=\"uint32\">720</height>\n" +
            "<!--热成像全景图 数据长度-->\n" +
            "<sourceBase64Length type=\"uint32\">85786</sourceBase64Length>\n" +
            "<!--热成像全景图 图片数据-->\n" +
            "<sourceBase64Data type=\"string\"><![CDATA[thermal full image data ...]]></sourceBase64Data>\n" +
            "</sourceDataInfo>\n" +
            "<listInfo type=\"list\" count=\"1\">\n" +
            "<item>\n" +
            "<targetId type=\"uint32\">0</targetId>\n" +
            "<rect>\n" +
            "<x1 type=\"uint32\">0</x1>\n" +
            "<y1 type=\"uint32\">0</y1>\n" +
            "<x2 type=\"uint32\">0</x2>\n" +
            "<y2 type=\"uint32\">0</y2>\n" +
            "</rect>\n" +
            "<targetImageData>\n" +
            "<dataType type=\"uint32\">0</dataType>\n" +
            "<targetType type=\"uint32\">0</targetType>\n" +
            "<width type=\"uint32\">720</width>\n" +
            "<height type=\"uint32\">720</height>\n" +
            "<targetBase64Length type=\"uint32\">54164</targetBase64Length>\n" +
            "<targetBase64Data type=\"string\"><![CDATA[thermal target image data ...]]></targetBase64Data>\n" +
            "</targetImageData>\n" +
            "</item>\n" +
            "</listInfo>\n" +
            "</thermalImageInfo>\n" +
            "</config>";

}
